package com.example.yyh.recorddemo.base;

import android.Manifest;
//import android.databinding.DataBindingUtil;
//import android.databinding.ViewDataBinding;
import android.os.Build;
import android.os.Bundle;
//import android.support.annotation.NonNull;
//import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;

import com.example.yyh.recorddemo.R;
import com.example.yyh.recorddemo.databinding.ActivityBaseBinding;
import com.example.yyh.recorddemo.permission.PermissionManager;
import com.yanzhenjie.permission.PermissionListener;

import java.util.List;

public abstract class BaseActivity<SV extends ViewDataBinding> extends FragmentActivity implements View.OnClickListener {

    protected SV mContentBindingView;//布局view
    protected ActivityBaseBinding mBaseBinding;//activity_base 布局的绑定view

    /**
     * 录音
     */
    protected String[] recordPermission = {
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE
    };

    /**
     * 写入文件权限
     */
    protected String[] storagePermission = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE};

    /**
     * 实现PermissionListener 得到权限的回调结果
     */
    private PermissionListener mListener = new PermissionListener() {
        /**
         * 授权成功
         *
         */
        @Override
        public void onSucceed(int requestCode, List<String> grantPermissions) {
            checkPermissionSuccess(requestCode);
        }

        /**
         * 授权失败
         *
         */
        @Override
        public void onFailed(int requestCode, List<String> deniedPermissions) {
            checkPermissionFailed(requestCode);
        }
    };

    /**
     * 权限开启失败或者拒绝
     *
     * @param requestCode
     */
    private void checkPermissionFailed(int requestCode) {
        //TODO: 2018/12/1 暂时没处理
    }

    /**
     * 权限开启成功 在子类处理结果
     *
     * @param requestCode
     */
    protected void checkPermissionSuccess(int requestCode) {

    }

    /**
     * 检测权限
     */
    protected void checkPermission(int checkRequestCode, @NonNull String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PermissionManager.requestPermission(this, checkRequestCode, permissions);
        } else {
            int permissionStatus = PermissionManager.permissionToOp(BaseActivity.this, permissions);
            if (permissionStatus == PermissionManager.ALLOWED) {
                checkPermissionSuccess(checkRequestCode);
            } else if (permissionStatus == PermissionManager.IGNORED) {
                checkPermissionFailed(checkRequestCode);
            }
        }
    }

    /**
     * 回调权限检测
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults, mListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(int layoutResID) {
        mBaseBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_base, null, false);
        mContentBindingView = DataBindingUtil.inflate(LayoutInflater.from(this), layoutResID, null, false);
        LinearLayout parentView = mBaseBinding.getRoot().findViewById(R.id.base_layout_parent);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mContentBindingView.getRoot().setLayoutParams(params);
        parentView.addView(mContentBindingView.getRoot());
        getWindow().setContentView(mBaseBinding.getRoot());
        initData();
        setListener();
    }


    /**
     * 子类处理数据
     */
    protected abstract void initData();

    /**
     * 子类设置监听回调
     */
    protected abstract void setListener();


    @Override
    public void onClick(View v) {

    }
}
